<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 订单详情 控制层
 * @author adela.xu 8.29
 */
class Login extends CI_Controller
{
    /**
     * 登录
     * @author Toby.tu 2016-08-30
     */
    public function index(){
        $filter = array('user_name'=>'','error_code'=>'');
        $this->load->view('admin/Login/index',$filter);
    }
    /**
     * 登录
     * @author Toby.tu 2016-08-30
     */
    public function signin(){
        try {
            $input = $this->input->post();
            if(!isset($input['user']) || !isset($input['user']['name'])
                    || !isset($input['user']['password'])){
                throw new Exception('请输入用户名或密码。');
            }
            $this->load->model('User_model','user_model');
            $where = array('name'=>$input['user']['name']);
            $user = $this->user_model->getOne($where);
            if(empty($user)){
                throw new Exception('未获取相关用户信息。');
            }
			$password = $input['user']['name'].$input['user']['password'];
            $pwd = $this->user_model->encodePasseord($password);
            if($pwd != $user['password']){
                throw new Exception('用户名或密码错误。');
            }
            if(0 == $user['status']){
                throw new Exception('用户被禁用，不能登录。');
            }
            $this->load->model('../logic/PermissionLogic','permis');
            unset($user['password']);unset($user['status']);
            $this->permis->setLoginUserInfo($user);
            redirect('admin/one/index');die;
        } catch (Exception $e) {
            $filter = array('user_name'=>$input['user']['name']);
            $filter['error_code'] = $e->getMessage();
            $this->load->view('admin/Login/index',$filter);
        }
    }
    /**
     * 注销
     * @author Toby.tu 2016-08-31
     */
    public function logout(){
        $this->load->model('../logic/PermissionLogic','permis');
        $this->permis->destoryLoginUserInfo();//注销
        redirect('admin/Login/index');die;
    }
	/**
	 * 重置密码
	 * @author Tujt 2017-10-10
	 */
	public function unsetPwd() {
	    $this->load->model('User_model','user_model');
		$where = array('name'=>'admin');
		$user = $this->user_model->getOne($where);
		if(!empty($user)){
			$update = array();
			$update['password'] = $this->user_model->encodePasseord('adminadmin');
			$this->user_model->updateById($user['id'],$update);
			echo 'SUCCESS';
		}
	}
}
